package com.wyp168.leetcode;

import java.util.Arrays;
import java.util.Objects;

/**
 * @ClassName MyCircularDeque
 * @Description TODO
 * @Author wyp168
 * @Date 2022/8/15 09:04
 */
public class MyCircularDeque {

    //聚合一个Object数组，初始化工作由构造器来完成
    Integer[] ints;

    public MyCircularDeque(int k) {
        ints = new Integer[k];
    }

    public boolean insertFront(int value) {
        int flag = calLength(this.ints);
        if (flag == ints.length - 1) {
            return false;
        }
        for (int i = flag + 1; i > 0; i--) {
            ints[i] = ints[i - 1];
        }
        ints[0] = value;
        return true;
    }

    public boolean insertLast(int value) {
        try {
            int index = calLength(ints);
            ints[index + 1] = value;
            return true;
        } catch (Exception e) {
            return false;
        }
    }

    public boolean deleteFront() {
        try {
            int flag = calLength(ints);
            for (int i = 0; i < flag; i++) {
                ints[i] = ints[i + 1];
            }
            ints[flag] = null;
            return true;
        } catch (Exception e) {
            return false;
        }
    }

    public boolean deleteLast() {
        try {
            int index = calLength(this.ints);
            this.ints[index] = null;
            return true;
        } catch (Exception e) {
            return false;
        }
    }

    public int getFront() {
        if (ints[0] == null) {
            return -1;
        } else {
            return ints[0];
        }
    }

    public int getRear() {
        int index = calLength(ints);
        if (index == -1) {
            return -1;
        }
        return ints[index];
    }

    public boolean isEmpty() {
        if (calLength(this.ints) == -1) {
            return true;
        } else {
            return false;
        }
    }

    public boolean isFull() {
        if (calLength(this.ints) == ints.length - 1) {
            return true;
        } else {
            return false;
        }
    }

    public int calLength(Integer[] ints) {
        if (ints[0] == null) {
            return -1;
        }
        if (ints[ints.length - 1] != null) {
            return ints.length - 1;
        }

        return findNull(ints,0,ints.length - 1);
    }

    public int findNull(Object[] objects, int start, int end) {
        int middle = (start + end) / 2;
        if (Objects.isNull(objects[middle])) {
            return findNull(objects, start, middle - 1);
        } else if (Objects.nonNull(objects[middle]) && Objects.nonNull(objects[middle + 1])) {
            return findNull(objects, middle + 1, end);
        } else {
            return middle;
        }
    }

    @Override
    public String toString() {
        return "MyCircularDeque{" +
                "ints=" + Arrays.toString(ints) +
                '}';
    }

    /**
 * Your MyCircularDeque object will be instantiated and called as such:
 * MyCircularDeque obj = new MyCircularDeque(k);
 * boolean param_1 = obj.insertFront(value);
 * boolean param_2 = obj.insertLast(value);
 * boolean param_3 = obj.deleteFront();
 * boolean param_4 = obj.deleteLast();
 * int param_5 = obj.getFront();
 * int param_6 = obj.getRear();
 * boolean param_7 = obj.isEmpty();
 * boolean param_8 = obj.isFull();
 */
}
